# -*- coding: utf-8 -*-
'''
@author: Hugo
@file: demo2.py
@time: 2021/8/21 18:10
'''
import os
from pprint import pprint

import requests

'''
https://www.paixin.com/mediapack/754/1

https://api2.paixin.com/albums/754/medias?page=0&size=45&sort=createdAt,desc

https://api2.paixin.com/albums/754/medias?page=1&size=45&sort=createdAt,desc

'''

'''
url = 'https://api2.paixin.com/albums/754/medias?page=0&size=45&sort=createdAt,desc'
res = Spider().fetch(url = url,headers = headers)
pprint(res.json())
'''
from utils.base import Spider
from icecream import ic
import redis

conn = redis.Redis()
from loguru import logger

def request(url):
    headers = {
        'Refeter': 'https://www.paixin.com/',
        'User-Agent': Spider.get_ua(),
    }
    # 判断url是否在redis里
    if conn.sadd('fff',url):
        res = requests.get(url,headers = headers)
        return res
    else:
        logger.info(url+':已经爬过了')

def run():
    page = 0
    pages = 1
    count = 0
    while page <= pages:
        url = f'https://api2.paixin.com/albums/754/medias?page={page}&size=45&sort=createdAt,desc'
        res = request(url)
        if res != None and 'elements' in res.text:
            data = res.json()
            for index,i in enumerate(data['elements']):
                title = i.get('id')
                img_url = i.get('image')
                pages = data.get('pages')
                ic(pages)
                if os.path.exists(os.getcwd()+'images') is False:
                    os.mkdir('images')
                save_path = os.path.join(os.getcwd()
                                         + 'images\\' + str(title) + '.jpg')
                res = request('http:' + img_url)
                with open(save_path,'wb') as f:
                    f.write(res.content)
                logger.info(save_path + f'>>>第{count}张图片done')
                count += 1
            page +=1
        else:
            logger.info(f'第{page}页爬完了')
            count += 1
            ic(count)
            page +=1

if __name__ == '__main__':
    run()






















